<?php

use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$institution_code = Mall::getCurrentInstitutionCode();
$this->title = $site_name.'->购物袋';
?>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall/cart.css"/>



<!------------------------------->
<div class="shop-bag">
    <div class="classify">
        <ul>
            <li class="btn1"><label class="checkbox fl"><input id="all_checkbox" class="choice-checkbox" type="checkbox">全选</label></li>
            <li class="classify-num">共<span id="totalNum">（0）</span>件商品</li>
            <li>单价</li>
            <li>数量</li>
            <li>小计</li>
            <li>操作</li>
        </ul>
    </div>
    <script type="text/javascript">
        function SetCartQuantity(cartId, quantity) {
            var data = {};
            data.cartId = cartId;
            data.quantity = quantity;
            requestJsonMallM('set-cart-quantity-handle', data);
        }
        removeCartGoods = function (me) {
            if (confirm("你确定删除这个商品吗？")==false){
                return;
            }
            var data = {};
            data.cartId = $(me).closest('.listing').attr('cartId');
            requestJsonMallM('remove-cart-handle', data);
            window.location.reload();
        };
        $(function() {
            $(".add").click(function() {
                var stock = $(this).closest('.goods-number').attr('stock');
//                alert(stock);return;
                var n = $(this).prev().val();
                var num = parseInt(n) + 1;
                if (num == 0) {
                    return;
                }
                if (num > 1) {
                    $(this).siblings(".jian").css("color", "#8a8a8a");
                }
                if (num > stock) {
                    $(this).css("color", "#E2E2E2");
                    return;
                }
                SetCartQuantity($(this).closest('.listing').attr('cartId'), num);
                $(this).prev().val(num);
                calcTotal();
            });
            $(".jian").click(function() {
                var stock = $(this).closest('.goods-number').attr('stock');
                var n = $(this).next().val();
                var num = parseInt(n) - 1;
                if (num == 0) {
                    return
                }
                if (num == 1) {
                    $(this).css("color", "#E2E2E2");
                }
                if (num < stock) {
                    $(this).siblings(".add").css("color", "#8a8a8a");
                }
                SetCartQuantity($(this).closest('.listing').attr('cartId'), num);
                $(this).next().val(num);
                calcTotal();
            });

            function setCheckAllStatus() {
                checked_number = 0;
                var doms = $(".listing .choice-checkbox");
                doms.each(function (index, item) {
                    if ($(item).prop('checked')) {
                        checked_number += 1;
                    }
                    if (checked_number == doms.length) {
                        $("#all_checkbox").prop('checked', true);
                    }
                });
                calcTotal();
            }
            setCheckAllStatus();
            calcTotal();
        });
        //计算合计
        function calcTotal() {
            var total_price = 0;
            var total_num=0;
            $.each($(".listing"), function (index, item) {
                var check_input = $(item).find('.choice-checkbox');
                var price = $(item).find('.statistics [name="goods_price"]').html();
                var num = $(item).find('.num').val();
                var goodsItemsPrice=parseInt(price) * parseInt(num);
                if (check_input.is(':checked') == true) {
                    total_price += goodsItemsPrice;
                    total_num=total_num+parseInt(num);
                }
                $(item).find('[name="goodsItemsPrice"]').html(goodsItemsPrice+'<?=$integral_unit?>');
            });
            $('#totalPrice').html(total_price);
            $('#totalNum').html('('+total_num+')');
            //同步页头购物车商品的数量
            refreshCartGoodsCount();
        }
    </script>
    <!------------------->
    <?php
    foreach ($cartList as $item) {
        $isSelected = '';
        if ($item['is_selected'] == 1) {
            $isSelected = 'checked="true"';
        }
    ?>
        <div class="listing" cartId="<?= $item['id'] ?>">
            <label class="checkbox fl">
                <input class="choice-checkbox" type="checkbox" <?= $isSelected ?>/>
            </label>
            <div class="goods-img fl">
                <a href="goods-detail?goods_code=<?= $item['goods_code'] ?>&institution_code=<?= $institution_code ?>">
                    <img src="<?= $item['goods_image'] ?>@120w"/>
                </a>
            </div>
            <div class="goods-name fl">
                <p><?php
                    if($item['is_crossborder']=='1'){
                        ?>
                        <span class="cross-title-icon">跨境</span>
                        <?php
                    }
                    ?>
                    <?= $item['name'] ?> </p>
            </div>
            <div class="statistics">
                <ul>
                    <li name="goods_price"><?= intval($item['selling_integral']) ?><?= $integral_unit ?></li>
                    <li>
                        <div class="goods-number" stock="<?= $item['stock'] ?>">
                            <div class="gw-num fl">
                                <span class="jian" style="color: <?= $item['quantity'] <= 1 ? '#E2E2E2' : '#8a8a8a' ?>;">-</span>
                                <input type="text" readonly="readonly" value="<?= $item['quantity'] ?>" class="num" />
                                <span class="add" style="color: <?= $item['quantity'] >= $item['stock'] ? '#E2E2E2' : '#8a8a8a' ?>;">+</span>
                            </div>
                        </div>
                    </li>
                    <li name="goodsItemsPrice">0</li>
                    <li><input type="button" value="删除" onclick="removeCartGoods(this)" /></li>
                </ul>
            </div>

        </div>
    <?php
    }
    ?>

    <!------------------>
    <!--<div class="listing">
        <label class="checkbox fl"><input class="choice-checkbox" type="checkbox"></label>
        <div class="goods-img fl">
            <a href=""><img src="imgs/wares1.png" /></a>
        </div>
        <div class="goods-name fl">
            <p>多功能书籍平板两用折叠支架书籍平板两用折叠支架 </p>
        </div>
        <div class="statistics">
            <ul>
                <li>289.9</li>
                <li>
                    <div class="goods-number">
                        <div class="gw-num fl">
                            <span class="jian">-</span>
                            <input type="text" value="1" class="num" />
                            <span class="add">+</span>
                        </div>
                    </div>
                </li>
                <li>289.9</li>
                <li><input type="button" value="删除" /></li>
            </ul>
        </div>
    </div>-->
    <div class="settle">
        <ul>
            <li>
                <a href="index?institution_code=<?= $institution_code ?>">
                    <<继续购物</a>
            </li>
            <li style="visibility: hidden;">活动优惠<span>-20.00</span></li>
            <li>共计<?=$integral_unit?><span id="totalPrice">289.9</span></li>
            <li><input type="button" value="去结算" onclick="submitOrder()" /></li>
        </ul>
    </div>
    <script>
        function submitOrder(){
            var doms = $(".listing .choice-checkbox:checked");
            if (doms.length <= 0) {
                alert('没有选择需要生成订单的商品!');
                return;
            } else {
                doms = $(".listing .choice-checkbox");
                $.each(doms, function (i, item) {
                    var data = {};
                    data.cartId = $(item).closest('.listing').attr('cartId');
                    data.isSelected = $(item).is(':checked') ? 1 : 0;
                    requestJsonMallM('set-cart-selected-handle', data);
                });
                window.location.href='order-form?institution_code=<?= $institution_code ?>';
            }

        }
        $(function() {
            var checked_number = 0;
            var input_checkbox = $(".listing .choice-checkbox");
            $("#all_checkbox").click(function() {
                if ($("#all_checkbox").is(':checked') == true) {
                    input_checkbox.prop('checked', true);
                } else {
                    $(".listing .choice-checkbox").prop('checked', false);
                }
                calcTotal();
            })
            input_checkbox.each(function(index) {
                $(this).click(function() {
                    if ($(this).is(':checked') == false) {
                        $("#all_checkbox").prop('checked', false);
                    }
                    checked_number = 0;
                    input_checkbox.each(function(index) {
                        if ($(this).is(':checked') == true) {
                            checked_number += 1;
                        }
                        if (checked_number == input_checkbox.length) {
                            $("#all_checkbox").prop('checked', true);
                        }
                    });
                    calcTotal();
                });
            });
        })
    </script>
</div>

<div class="show">
    <p class="look-history">猜你喜欢</p>
    <ul>
        <?php
        foreach ($hotGoosList as $item) {
        ?>
            <li >
                <a class="goods-item" href="goods-detail?goods_code=<?= $item['code'] ?>&institution_code=<?= $institution_code ?>">
                    <img src="<?= $item['image_url'] ?>@300w">
                    <p>
                        <?php
                        if($item['is_crossborder']=='1'){
                            ?>
                            <span class="cross-title-icon">跨境</span>
                            <?php
                        }
                        ?>

                        <?= $item['name'] ?>
                    </p>
                    <p class="integral-num">
                        <!--<span class="old" >599 积分</span>-->
                        <span class="now"><span><?= intval($item['selling_integral']) ?></span>&nbsp;&nbsp;<?= $item['integral_unit'] ?></span>
                    </p>
                </a>
            </li>
        <?php
        }
        ?>
     <!--   <li >
            <a href="">
                <img src="imgs/goods.jpg" />
                <p>大牌时尚羊皮双肩包大牌时尚羊皮双肩包大牌时尚羊皮双肩包大牌时尚羊皮双肩包</p>
                <p class="integral-num">
                    <span class="old">599 积分</span>
                    <span class="now"><span>199</span>&nbsp;&nbsp;积分</span>
                </p>
            </a>
        </li>-->

    </ul>
    <div class="clfl"></div>
    <script>
        $(function() {
            $(".history ul li .rmqgm").hide();
            $(".history ul li").mouseover(function() {
                $(this).find(".rmqgm").show();
            })
            $(".history ul li").mouseleave(function() {
                $(this).find(".rmqgm").hide();
            })
        })
    </script>
</div>
